package com.appspot.checkflames.servlet;

import static com.appspot.checkflames.constants.CheckFlamesConstants.ACCESS_TOKEN_PARAM;
import static com.appspot.checkflames.constants.CheckFlamesConstants.FLAMES_MAP;
import static com.appspot.checkflames.constants.CheckFlamesConstants.FLAMES_MAP_RESULT;
import static com.appspot.checkflames.constants.CheckFlamesConstants.FLAMES_RESULT;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.appspot.checkflames.bean.Statistics;
import com.appspot.checkflames.service.UserStatisticsServiceImpl;
import com.appspot.checkflames.utils.FlamesUtils;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.FacebookException;
import com.restfb.FacebookJsonMappingException;
import com.restfb.Parameter;
import com.restfb.types.User;

public class CalculateFlames extends HttpServlet {

	/**
	 * Generated serial version uid.
	 */
	private static final long serialVersionUID = -5256096717505493329L;

	private static final Logger LOGGER = Logger.getLogger(CalculateFlames.class);
	
	private UserStatisticsServiceImpl userStatisticsService = new UserStatisticsServiceImpl();
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException,ServletException {
		
		String accessToken = req.getParameter(ACCESS_TOKEN_PARAM);
		
		FacebookClient client = new DefaultFacebookClient(accessToken);
		
		String friendId = req.getParameter("selectedfriend");
		LOGGER.info("FETCHING FRIEND DETAILS OF " + friendId);
		User friendDetail = null;
		User myDetail = null;
		try {
			friendDetail = client.fetchObject(friendId, User.class, Parameter.with("fields", "name,picture"));
			myDetail = client.fetchObject("me", User.class, Parameter.with("fields", "name,picture"));
		} catch (FacebookJsonMappingException pException) {
			LOGGER.error("Error in parsing the content", pException);
		} catch (FacebookException pException) {
			LOGGER.error("Error in facebook retrieval", pException);
		}
		if(myDetail == null || friendDetail == null) {
			req.getSession().setAttribute(ACCESS_TOKEN_PARAM, null);
			resp.sendRedirect("/checkflames/");
			return;
		}
		int flamesResult = FlamesUtils.calculateFlames(myDetail.getName(), friendDetail.getName());
		Statistics statistics = new Statistics();
		statistics.setFriendId(friendId);
		statistics.setFriendName(friendDetail.getName());
		statistics.setUserName(myDetail.getName());
		statistics.setUserUid(myDetail.getId());
		statistics.setResult(flamesResult+1);
		statistics.setCreatedDate(new Date());
		userStatisticsService.persistUserStatistics(statistics);
		req.setAttribute("flames_map", FLAMES_MAP);
		req.setAttribute(FLAMES_MAP_RESULT, FLAMES_MAP[flamesResult]);
		req.setAttribute(FLAMES_RESULT, flamesResult+1);
		req.setAttribute("mydetail", myDetail);
		req.setAttribute("friend", friendDetail);
		req.getRequestDispatcher("flamesresult.jsp").forward(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
		doGet(req,resp);
	}
}
